//
//  WFChannelWebViewController.m
//  FAKER
//
//  Created by Jianwei on 15/11/11.
//  Copyright © 2015年 YHLai. All rights reserved.
//

#import "WFChannelWebViewController.h"
#import "BaseClassWebView.h"
#import "AllHeader.h"
#import "FitHeader.h"
#import "WFModelSingleton.h"

@interface WFChannelWebViewController ()<UIScrollViewDelegate, UIWebViewDelegate>
@property(nonatomic, strong)BaseClassWebView *webView;
@property(nonatomic, strong)UIView *topView;
@property(nonatomic, assign)CGFloat heightOfTopView;
@property(nonatomic, strong)UIView *statusBarView;
@property(nonatomic, strong)YHLoadingView *loadingView;
@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UILabel *autherLabel;
@property(nonatomic, strong)UIColor *currentColor;
@property(nonatomic, strong)UIColor *fontColor;
@end

@implementation WFChannelWebViewController
- (void)viewDidAppear:(BOOL)animated
{
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //创建加载中视图
    _loadingView = [[YHLoadingView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self.view addSubview:_loadingView];
}

#pragma mark - 创建头视图
- (void)createTopView
{
    _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, _heightOfTopView)];
//    if ([[WFModelSingleton shareInstance] model]) {//根据单例改变头视图颜色
//        _topView.backgroundColor = [UIColor RGBFromHexColor:[[[[WFModelSingleton shareInstance] model] block_info] objectForKey:@"block_color"]];
//    }
//    else {
//        _topView.backgroundColor = [UIColor darkGrayColor];
//    }
    _topView.backgroundColor = _currentColor;
    [_webView.scrollView addSubview:_topView];
    
    //创建标题label
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 30, _heightOfTopView - 120)];
    _titleLabel.center = _topView.center;
    _titleLabel.textColor = _fontColor;
    _titleLabel.text = _model.title;
    _titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:23];
    _titleLabel.numberOfLines = 0;
    [_topView addSubview:_titleLabel];
    
    //创建作者label
    _autherLabel = [[UILabel alloc] initWithFrame:CGRectMake(_titleLabel.X, _titleLabel.Y + _titleLabel.HEIGHT + 5, _titleLabel.WIDTH, 20)];
    _autherLabel.textColor = _fontColor;
    _autherLabel.text = _model.auther_name;
    _autherLabel.font = [UIFont fontWithName:@"Helvetica" size:13];//Helvetica-Bold
    [_topView addSubview:_autherLabel];
    
    //创建状态栏背景view
    _statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
//    if ([[WFModelSingleton shareInstance] model]) {
//        _statusBarView.backgroundColor = [UIColor RGBFromHexColor:[[[[WFModelSingleton shareInstance] model] block_info] objectForKey:@"block_color"]];
//    }
//    else {
//        _statusBarView.backgroundColor = [UIColor darkGrayColor];
//    }
    _statusBarView.backgroundColor = _currentColor;
    [self.view addSubview:_statusBarView];
}

#pragma mark - 布局
- (void)loadView
{
    [super loadView];
    
    //创建webView
    _webView = [[BaseClassWebView alloc]initWithFrame:CGRectMake(0, -20, SCREEN_WIDTH, SCREEN_HEIGHT + 20 + 2)];
    _webView.scrollView.delegate = self;
    _webView.delegate = self;
    _webView.scrollView.bounces = NO;
    [self.view addSubview:_webView];
    
//    dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//    dispatch_async(globalQueue, ^{
        //根据夜间模式改颜色
        [self changeColor];
        //判断传进来的model是否为空值
        if (![_model.full_url isEqualToString:@""]) {
            //获取title的高度
            _heightOfTopView = [Adaptive AdaptiveHeightByString:_model.title Width:SCREEN_WIDTH - 30 FontSize:23] + 120;
            //修改webView的frame
            _webView.scrollView.subviews.firstObject.frame = CGRectMake(0, _heightOfTopView, SCREEN_WIDTH, SCREEN_HEIGHT - _heightOfTopView);
            //创建头视图
            [self createTopView];
        } else {
            _model.full_url = @"http://weibo.com/u/5290447258";
        }
//    });
//    dispatch_async(globalQueue, ^{
        //获取网页的htmlString并加载
        [InternetDataRequest getDataByString:_model.full_url BodyDic:nil WithDataBlock:^(id data) {
            NSString *htmlString = [NSString changeHTMLStringWithDataDictionary:data];
            [_webView loadHTMLString:htmlString baseURL:nil];
        }];
//    });
}

#pragma mark - 夜间模式改颜色
- (void)changeColor
{
    if (NSUSERDEFAULTS_NIGHT) {
        _fontColor = [UIColor lightGrayColor];
        _currentColor = BGCOLOR_BLACK;
    } else {
        _fontColor = [UIColor whiteColor];
        if (![[WFModelSingleton shareInstance] model]) {
            _currentColor = [UIColor darkGrayColor];
            return;
        }
        _currentColor = [UIColor RGBFromHexColor:[[[[WFModelSingleton shareInstance] model] block_info] objectForKey:@"block_color"]];
    }
}

#pragma mark - webView协议方法
#pragma mark 点击图片
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if ([request.URL.absoluteString isEqualToString:@"http://www.myzaker.com/m/"]) {
        return NO;
    }
    return YES;
}
#pragma mark 已经开始加载
- (void)webViewDidStartLoad:(UIWebView *)webView
{
    if (NSUSERDEFAULTS_NIGHT) {
        [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#1F2121'"];
        [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor='gray'"];
    }
}
#pragma mark 完成加载
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [_loadingView removeFromSuperview];
    
    if (NSUSERDEFAULTS_NIGHT) {
        [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#1F2121'"];
        [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor='gray'"];
    }
}

#pragma mark - scrollView协议方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    NSLog(@"%f", 20 - (scrollView.contentOffset.y + 20));
    if (scrollView.contentOffset.y <= -20) {
        _statusBarView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 20 - (scrollView.contentOffset.y + 20));
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
